package mk.jug.academy.util;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Common utility class.
 *
 * @author Ice.Penov
 */
public final class Utils {
	
	private static final Logger LOG = Logger.getLogger(Utils.class.getName());
	
	//TODO: Externalize this - Sending account.
	private static final String SENDER_EMAIL = "jugmk.academy@gmail.com";
	private static final String SENDER_NAME = "JUGMK Academy";
	
	private Utils() {;}
		
	/**
	 * Tokenizes the given items using a token.
	 * 
	 * List of [1,2,3] -> string of 1,2,3; of , is the token.
	 */
	public static String tokenize(List<?> items, String token) {
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (Object item : items) {
			if (i > 0) {				
				sb.append(",");
			}
			sb.append(item.toString());
			i++;
		}
		return sb.toString();
	}
	
	/**
	 * Calculates the start position given the pageNumber
	 * and the number of items per page.
	 */
	public static int calculateStart(int pageNumber, int itemsPerPage) {
		return (pageNumber - 1) * itemsPerPage;
	}
	
	/**
	 * Calculates the total page number given the parameters.
	 * 
	 * @param itemsPerPage the number of items on a page
	 * @param totalCount the total count of all items
	 * @return the page number
	 */
	public static int getPageNumber(int itemsPerPage, int totalCount) {
		int i = totalCount / itemsPerPage;
		if ((totalCount % itemsPerPage != 0) && totalCount > itemsPerPage * i) {
			i++;
		}
		return i;
	}
	
	/**
	 * Sends a mail using the GAE infrastructure.
	 * 
	 * @param to the sender; index 0 - email, index 1 - personal name (optional)
	 * @param subject the subject of the mail
	 * @param body the contents of the mail
	 */
	public static void sendMail(String[] to, String subject, String body) {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		
		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(SENDER_EMAIL, SENDER_NAME));
			InternetAddress inetTo = null;
			if (to.length == 1) {
				inetTo = new InternetAddress(to[0]);
			} else if (to.length == 2) {
				inetTo = new InternetAddress(to[0], to[1]);
			}
			msg.addRecipient(RecipientType.TO, inetTo);
			msg.setSubject(subject);
			msg.setText(body);
			Transport.send(msg);
			LOG.info("Mail send to [" + to + "] with subject [" + subject + "].");
		} catch (AddressException e) {
			LOG.log(Level.SEVERE, "Error occured while sending mail.", e);
		} catch (MessagingException e) {
			LOG.log(Level.SEVERE, "Error occured while sending mail.", e);
		} catch (UnsupportedEncodingException e) {
			LOG.log(Level.SEVERE, "Error occured while sending mail.", e);
		}
	}
	
}